<?php
namespace app\Admin\controller;
use app\admin\controller\Base;

class Picture extends Base
{
    public function index()
    {
    	$Picture=Model('Picture');
        $Pictureres=$Picture->order('id')->paginate(3);
        $this->assign('list',$Pictureres);
        return view();
    }

    public function add()
    {	
    	if(request()->isPost()){
            $data=input('post.');
            $data['addtime']=time();
            $Picture=Model('Picture');
            if($Picture->save($data)){
                $this->success('添加图片成功',url('index'));
            }else{
                $this->error('添加图片失败！');
            }
            return;
        }
        return view();
    }

    public function edit(){
    	 if(request()->isPost()){
            $data=input('post.');
            $Picture=Model('Picture');
            $save=$Picture->update($data);
            if($save){
                $this->success('修改图片成功！',url('index'));
            }else{
                $this->error('修改图片失败！');
            }
            return;
        }
        $list=db('Picture')->where(array('id'=>input('id')))->find();
        $this->assign('list',$list);
        return view();
    }

    public function onstatus()
    {
        $id = input('id');
        $Picture = db('Picture')->find($id);
        if ($Picture['status'] == 1) {
            $data = [
                'id' => $id,
                'status' => 0,
            ];
            db('Picture')->update($data);
            $this->redirect('index');
        } else {
            $data = [
                'id' => $id,
                'status' => 1,
            ];
            db('Picture')->update($data);
            $this->redirect('index');
        }
    }

    public function ontype()
    {
        $id = input('id');
        $Picture = db('Picture')->find($id);
        if ($Picture['type'] == 1) {
            $data = [
                'id' => $id,
                'type' => 0,
            ];
            db('Picture')->update($data);
            $this->redirect('index');
        } else {
            $data = [
                'id' => $id,
                'type' => 1,
            ];
            db('Picture')->update($data);
            $this->redirect('index');
        }
    }

    public function del(){
    	$id=input('id');
		if(db('Picture')->delete(input('id'))){
			$this->success('删除图片成功！','index');
		}else{
			$this->error('删除图片失败！');
		}
    	
    }



}
